﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;

namespace VisualTest2
{
    class PointOperations
    {
        public static Point getPolylinePoint(PointCollection coll, double x)
        {
            int start = 0;
            int end = coll.Count - 1;
            if (end < 1) return coll[end];

            int current = start + 1;
            int step = 0;
            while (true)
            {
                step++;
                if (coll[current].X < x && coll[current + 1].X > x)
                {
                    //Debug.WriteLine("Step : " + step);
                    return coll[start];
                }
                current = start + (end - start) / 2;
                if (coll[current].X > x)
                    end = current;
                else start = current;
                if (Math.Abs(start - end) < 2)
                {
                    //Debug.WriteLine("Step : " + step);
                     return coll[start];
                }
            }
        }
        public PointCollection createRandomCollection(Size newSize,int count) {
            
            PointCollection collection = new PointCollection();
            Random r = new Random();

            double ratio = newSize.Width / count;
            collection.Add(new Point(0, newSize.Height));
            for (int i = 1; i < count; i++)
            {
                double randY = Math.Round((r.NextDouble() * (newSize.Height)), 0);
                if (randY <= 16)
                {
                    randY += 10;
                }
                collection.Add(new Point(Math.Round((i * ratio), 0), randY));
            }
            collection.Add(new Point(newSize.Width, newSize.Height));
            return collection;
        }
    }
}
